package com.gettagger.tagger.activities;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

import com.gettagger.tagger.R;
import com.gettagger.tagger.models.GuardianObject;
import com.gettagger.tagger.models.TaggerConstants;
import com.google.gson.Gson;

public class ActUserInfo extends Activity {

	private SharedPreferences sharedPref;
	private TextView txtName=null;
	private TextView txtAddress=null;
	private TextView txtAge=null;
	private TextView txtBirthday=null;
	private TextView txtGender=null;
	private TextView txtLastUpdated=null;
	private TextView txtContactNo=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE); 
		setContentView(R.layout.act_user_info);

		sharedPref = getSharedPreferences(TaggerConstants.PREFERENCE_NAME, Context.MODE_PRIVATE);
		txtName = (TextView)findViewById(R.id.userinfo_txt_name);
		txtAddress = (TextView)findViewById(R.id.userinfo_txt_address);
		txtAge = (TextView)findViewById(R.id.userinfo_txt_age);
		txtBirthday = (TextView)findViewById(R.id.userinfo_txt_birthday);
		txtGender = (TextView)findViewById(R.id.userinfo_txt_gender);
		txtLastUpdated = (TextView)findViewById(R.id.userinfo_txt_last_updated);
		txtContactNo = (TextView)findViewById(R.id.userinfo_txt_contactno);
		
		String contents = sharedPref.getString(TaggerConstants.PREFERENCE_USER_INFO,null);
		if (contents!=null){
			fillData(contents);
		}
		
		
		TextView txtLogout = (TextView)findViewById(R.id.userinfo_txt_logout);
		txtLogout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				logout();
				finish();
				Intent intent = new Intent(ActUserInfo.this, ActLogin.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
				startActivity(intent);
			}
		});
		TextView txtBack = (TextView)findViewById(R.id.userinfo_txt_back);
		txtBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	
	private void fillData(String contents) {
		Gson gson = new Gson();
		GuardianObject guardianObj = null;
		
		guardianObj = gson.fromJson(contents, GuardianObject.class);
		if(guardianObj!=null){
			txtName.setText(""+guardianObj.last_name+","+guardianObj.first_name+" "+guardianObj.middle_name);
			txtAddress.setText(""+guardianObj.address);
			txtAge.setText(""+guardianObj.age);
			txtBirthday.setText(""+rearrangeDateFormat(guardianObj.birthday));
			txtGender.setText(""+guardianObj.gender);
			txtLastUpdated.setText(""+rearrangeDateFormat(guardianObj.last_updated));
			txtContactNo.setText(""+guardianObj.contact_number);
		}
	}
	
	private String rearrangeDateFormat(String dateString){
		
		String returnString=null;
		
		java.util.Date date=null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		returnString = new SimpleDateFormat("MMM d, yyyy").format(date);
		return returnString;
	}


	private void logout() {
		Editor editor = getSharedPreferences(TaggerConstants.PREFERENCE_NAME, Context.MODE_PRIVATE).edit();
		editor.putBoolean(TaggerConstants.PREFERENCE_SESSION, false);
		editor.commit();
	}
	
	
}
